﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using Shorten.Core;
using Shorten.Core.Models;
using Shorten.Core.Providers;
using Shorten.Core.Helpers;

namespace Shorten.Parts
{
    /// <summary>
    /// Summary description for Shorten
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    [System.Web.Script.Services.ScriptService]
    public class Shorten : System.Web.Services.WebService
    {


        [WebMethod]
        public string CreateShortUrl(string LongUrl, string CustomName)
        {
            if (!String.IsNullOrEmpty(LongUrl))
            {

                ShortenItems items = new ShortenItems();
                items.LongUrl = LongUrl;
                items.ShortUrl = ShortenManagement.GetShortUrlByLongUrl(items.LongUrl);

                //Check if the URL already exists in the database, if it does, then return the existing short URL
                if (!String.IsNullOrEmpty(items.ShortUrl) && items.ShortUrl.Length > 0)
                {
                    //Yes the long url already exists so just return the short one
                    if (CustomName == items.ShortUrl)
                        return Utility.GetSiteUrl() + LogItem.AddToLog(items, LogItem.ActionType.TryCreateCustomButExists, null);
                    else
                        return Utility.GetSiteUrl() + LogItem.AddToLog(items, LogItem.ActionType.TryCreateButExists, null);
                }

                //Test for the custom URL
                if (!String.IsNullOrEmpty(CustomName) && CustomName.Length > 0)
                {
                    //exist = ShortenManagement.CheckIfShortenUrlExist(CustomName);
                    items.ShortUrl = CustomName;
                    try
                    {
                        ShortenManagement.AddShortenUrl(items);
                        return Utility.GetSiteUrl() + LogItem.AddToLog(items, LogItem.ActionType.Create, null);
                    }
                    catch (Exception ex)
                    {
                        LogItem.AddToLog(items, LogItem.ActionType.Error,ex.Message);
                        //return "Error: " + ex.Message;
                    }
                }

                int innercount = 0;

                //TODO - Find a better way to generate a new random url, currently tries 50 times before bombing out
                do
                {
                    items.ShortUrl = Utility.GetRandomShortenUrl(SettingsManagement.GetSiteURLLength()); ;
                    try
                    {
                        ShortenManagement.AddShortenUrl(items);
                        return Utility.GetSiteUrl() + LogItem.AddToLog(items, LogItem.ActionType.Create, null);
                    }
                    catch { }


                    innercount++;
                } while (innercount < 50);

            }
            return "Something whent wrong. Please try again";

        }
    }
}
